#!/usr/bin/env python
# encoding: utf-8
'''
@author: zhaowenpeng
@contact: winston@peipeiyun.com
@software: garner
@file: __init__.py.py
@time: 2020/8/10 6:25 下午
@desc:
'''
from .my_log import logger
import traceback
import ujson
import aiohttp

async def fetch(url, data=None,method='GET', timeout=3):
    async with aiohttp.ClientSession() as session:
        async with session.request(method,url, data=data,timeout=timeout) as response:
            try:
                chunk = await response.content.read()
                if chunk:
                    data = ujson.loads(chunk)
                else:
                    data = {'code': 0, 'data': {}, 'info': '无数据'}
                return data
            except Exception as e:
                logger.error(traceback.format_exc())
                data = {'code': 0, 'data': {}, 'info': str(e)}
                return data